<?php

class HTTPSocket {
	private $handle;
	private $host;
	private $port;
	public $error_string;
	public $error_number;
	
	/**
	*	Run when the class is created. Sets the variables
	*/
	function HTTPSocket ($host, $port) {
		$this->host = $host;
		$this->port = $port;
		
		// Open a connection in the class variable
		$this->handle = fsockopen($this->host, $this->port, $this->error_number, $this->error_string);
		if ( $this->handle ) {
			return TRUE;
		}
		else {
			return FALSE;
		}
	}
	
	/**
	*	Send data through the socket and listen for a return
	*/
	function send ($msg, $type = '') {
		$timeout=120;
		stream_set_timeout($handle,$timeout);
		
		fwrite($this->handle, $msg);
		
		if ( $type == 'array' ) {
			$response = array();
			$line_num = 0;
			while ( !feof($this->handle) ) {
	       			$response[$line_num] = fgets($this->handle, 4096);
				$line_num++;
	   		}
			return $response;
		}
		elseif ( $type == 'string' ) {
			$response = '';
	   		while ( !feof($this->handle) ) {
	       			$response .= fgets($this->handle, 4096);
	   		}
			return $response;
		}
		else {
			return TRUE;
		}
		
	}
	
	/**
	*	Closes the connection
	*/
	function close () {
		// Close connection
		fclose($this->handle);
		return TRUE;
	}
}

?>